<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    @ok="handleOk"
    @cancel="handleCancel"
    :okText="$t('common.okBtnTxt')"
    :cancelText="$t('common.cancelBtnTxt')">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('merchant.name')">
          <span className="ant-form-text">{{model.name}}</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('merchant.code')">
          <span className="ant-form-text">{{model.code}}</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('merchant.address')">
          <span className="ant-form-text">{{model.address}}</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('merchant.type')">
            <span className="ant-form-text">{{model.type_dictText}}</span>
        </a-form-item>
       <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="审核状态 0 待审核，1审核通过 2 审核不通过 ">
          <a-input placeholder="请输入审核状态 0 待审核，1审核通过 2 审核不通过 " v-decorator="['auditStatus', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="审核结果描述">
          <a-input placeholder="请输入审核结果描述" v-decorator="['auditDesc', {}]" />
        </a-form-item>-->
        <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="证件">
          <a-input placeholder="请输入证件" v-decorator="['certificates', validatorRules.certificates ]" :readOnly="disableSubmit"/>
        </a-form-item> -->

        <a-form-item  :label="$t('merchant.certificates')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!-- <a-upload
            listType="picture-card"
            class="avatar-uploader"
            :showUploadList="false"
            :action="uploadAction"
            :data="{'isup':1}"
            :headers="headers"
            :beforeUpload="beforeUpload"
            @change="handleChange"
          >
            <img v-if="picUrl" :src="getCertificates()" alt="证件" style="height:104px;max-width:300px"/>
            <div v-else>
              <a-icon :type="uploadLoading ? 'loading' : 'plus'" />
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload> -->
          <j-upload ref="uploadCertificates"
          :value="model.certificates" fileType=“image” @change="afterUpload" :count="1" :disabled="true" :showRemoveIcon="false"></j-upload>
          <a-input :placeholder="$t('merchant.inputCertificates')" v-decorator="['certificates', {}]" :hidden="true"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('merchant.conect')">
          <span className="ant-form-text">{{model.contactsName}}</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('merchant.conectMobile')">
                    <span className="ant-form-text">{{model.contactsMobile}}</span>

        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
         :label="$t('merchant.remark')">
                              <span className="ant-form-text">{{model.remark}}</span>

        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('merchant.auditStatus')">
                              <span className="ant-form-text">{{model.auditStatus_dictText}}</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="$t('merchant.auditDesc')">
                                        <span className="ant-form-text">{{model.auditDesc}}</span>

        </a-form-item>
		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import { httpAction } from '@/api/manage'
  import  JUpload from '@/components/jeecg/JUpload'
  import pick from 'lodash.pick'
  import moment from "moment"
  import Vue from 'vue'

  export default {
    name: "MerchantModal",
    components: {
      JUpload
    },
    data () {
      return {
        disableSubmit: false,
         title: this.$t('common.opBtnTxt'),
        visible: false,
        model: {certificates: ''},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        uploadLoading:false,
        confirmLoading: false,
        headers:{},
        picUrl: "",
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        name:{rules: [{ required: true, message: this.$t('merchant.inputName')  }]},
        code:{rules: [{ required: true, message: this.$t('merchant.inputCode') }]},
        certificates:{rules: [{ required: true, message: this.$t('merchant.inputCertificates')  }]},
        },
        url: {
          add: "/merchant/merchant/add",
          edit: "/merchant/merchant/edit",
          fileUpload: window._CONFIG['domianURL']+"/sys/common/upload",
          imgerver: window._CONFIG['domianURL']+"/sys/common/view"
        },
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token}      
    },
    computed:{
      uploadAction:function () {
        return this.url.fileUpload;
      }
    },
    methods: {
      afterUpload: function(path){
        console.log(path)
        this.model.certificates = path;
        this.form.setFieldsValue({
            'certificates': path
        });
      },
      add () {
        this.edit({});
      },
      edit (record) {
        // document.getElementsByClassName('ant-upload-select').style.display = 'none';
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        console.log(this.form)
        this.$nextTick(() => {
          this.form.setFieldsValue(
            pick(this.model,
            'name',
            'code',
            'address',
            'type',
            'auditStatus',
            'auditDesc',
            'certificates',
            'contactsName',
            'contactsMobile',
            'remark'
            )
            );
            
 
		      //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>
